<?php

class Core_Frontend_Module_Road_Latest extends Dcmp_Module_Base {
    protected $name = 'road_latest';

    public function install() {

    }

    public function uninstall() {

    }

    public function buildConfig($form) {
        $form->addTextBoxField($this->__('share.label.item_count'),'item_count','module[parameters][item_count]')
             ->addRequiredRule()
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_count'),'image_count','module[parameters][image_count]')
             ->setValue(0)
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_count'),'introduction_count','module[parameters][introduction_count]')
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_size'),'introduction_size','module[parameters][introduction_size]')
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_width'),'image_width','module[parameters][image_width]')
             ->setValue(0)
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_height'),'image_height','module[parameters][image_height]')
             ->setValue(0)
             ->addDigitsRule();
        $form->createTab('module_option', $this->__('design.label.module_parameter'),
                array( 'field_item_count', 'field_introduction_count'
                    , 'field_introduction_size', 'field_image_count','field_image_width', 'field_image_height'));
        
    }

    public function render() {
        $obj = new Kd_Object($this->getParameters());
        $roads = App::getModel('foundation/collection_road')
                    ->resetSelect()
                    ->field('a.*')
                    ->gt(Kd_Data_Dcl_Fn::stringLength('id'),10)
                    ->limit($obj->getItemCount())
                    ->load();
        $nameStartIndex = 0;
        $maxRecords = count($roads);
        if($obj->getImageCount()){
            $obj->setIsShowImage(TRUE);
            $obj->setStartImageIndex(0);
            $obj->setEndImageIndex(min($maxRecords, $obj->getImageCount()));
            $nameStartIndex = max($obj->getEndImageIndex(), $nameStartIndex);
        }
        if($obj->getIntroductionCount()){
            $obj->setIsShowIntroduction(TRUE);
            $obj->setStartIntroductionIndex(0);
            $obj->setEndIntroductionIndex(min($maxRecords, $obj->getIntroductionCount()));
            $nameStartIndex = max($obj->getEndIntroductionIndex(), $nameStartIndex);
        }
        $obj->setStartNameIndex($nameStartIndex);
        $obj->setEndNameIndex(min($maxRecords, $obj->getItemCount()));
        $tmpl = $this->getTemplate()->assignMulti(array('roads' => $roads, 'parameters'=>$obj));
        return $tmpl->render($this->getTemplatePath($obj->getTemplateName(), $obj->getTemplateCode()));
    }

}

